<?php

namespace App\Http\Middleware;

use App\Consts\GlobalConst;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class PcOperatingLog
{
    /**
     * Handle an incoming request.
     * 记录操作日志
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        $userInfo = (object)collect($request->get('LOGIN_STATE_INFO'))->toArray();
        $loadData = [
            'tourist_id' => $userInfo->identity === GlobalConst::IDENTITY_TOURIST ? $userInfo->userId : '',
            'user_id' => $userInfo->userId,
            'identity' => $userInfo->identity,
            'token' => $request->cookie('token') ? : $request->header('token'),
            'route' => $request->getUri(),
            'method' => strtoupper($request->method()),
            'params' => json_encode($request->all(), JSON_UNESCAPED_UNICODE),
            'code' => response('code')->getStatusCode(),
            'referer' => $request->header('referer'),
            'ip' => $request->ip(),
            'created_at' => strtotime(date('Y-m-d H:i:s')),
            'updated_date' => date('Y-m-d H:i:s'),
        ];
        $json = json_encode($loadData, JSON_UNESCAPED_UNICODE);
        //$base64Info = base64_encode($json);
        Log::channel('operate')->info($json);
        return $next($request);
    }
}
